package com.cisetech.mapper.xml;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.io.IOUtils;

public class ToXmlWeb {
	/**
	 * 访问接口调用的方法
	 * @throws IOException 
	 */
	public String toXmlFrom(String xml){
        try {
        	HttpURLConnection conn = null;
            // 访问地址
            URL url;
        	 // 打开连接及基本属性设置
        	url = new URL("http://119.38.194.228:36288/webInter/Inter-service.do");
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod("POST");
	        conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
	        conn.setDoInput(true);
	        conn.setDoOutput(true);
	        // 设置交易代码
//		            conn.setRequestProperty("GW_CH_TX", "1102");
	        // 这里request是请求报文内容，演示程序里为从文件读取
	        String request = xml;//"<?xml version=\"1.0\" encoding=\"UTF-8\"?> <request>   <requestHead>     <consumerSeqNo>0000000001</consumerSeqNo>     <consumerID>test</consumerID>     <requestType>CarModelInfoInquiry</requestType>     <passWord></passWord>     <channelType>01</channelType>     <product>01</product>     <regionCode>31000000</regionCode>     <version>1</version>   </requestHead>   <requestBody>     <licenseNo></licenseNo>     <engineNo>EN55667788</engineNo>     <frameNo>FN777666888098765</frameNo>     <vinNo>FN777666888098765</vinNo>     <vehicleBrand>东风</vehicleBrand>     <vehicleModel>东风雪铁龙DC7205</vehicleModel>     <enrollDate>2015-01-01</enrollDate>     <carkindcode></carkindcode>     <startdate>2015-10-01</startdate>     <industryModelCode></industryModelCode>     <carType2>01</carType2>     <carType3>067</carType3>     <replacement></replacement>   </requestBody> </request>";
	        System.out.println("------ Request ------");
	        System.out.println(request);
	        // 写入请求报文
	        OutputStreamWriter writer = new OutputStreamWriter(
	                conn.getOutputStream(), "UTF-8");
	        IOUtils.write(request, writer);
	        writer.close();

	        // 读取响应报文
	        InputStream in = conn.getInputStream();
	        System.out.println("------ Response ------");
	        String response = IOUtils
	                .toString(new InputStreamReader(in, "UTF-8"));
	        System.out.println(response);
	        if (conn != null) {
	            conn.disconnect();
	        }
	        return response;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
        
	}

}
